/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author hans
 */
public class DatabaseConnection {

    private Statement stmt;
    private Connection conn;
    private ResultSet rs;
    private String sql;
    private String db;
    private String url;
    private String user;
    private String password;

    public DatabaseConnection(String host, String port, String db, String user, String password) throws ClassNotFoundException, SQLException {
        this.db = db;
        this.url = "jdbc:mysql://" + host + ":" + port + "/" + db;
        this.user = user;
        this.password = password;
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
    }

    public ResultSet selectQuery(String sql) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        rs = stmt.executeQuery(sql);
        return rs;
    }

    public void query(String sql) throws SQLException {
        stmt.execute(sql);
    }

    public void close() throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}
